local UIBase = require("view.UIBase")
local PopupStoryChat = class("PopupStoryChat", UIBase)
local GameUtils = require("utils.GameUtils")
local StoryHelper = require("gameLogic.story.StoryHelper")
local UIUtil = require("utils.UIUtil")
local Vector3 = require("utils.Vector3")

function PopupStoryChat:ctor()
    local resource = {
        path = "Prefabs/Popup",
        name = "PopupStoryChat",
        canvas = 3
    }
    PopupStoryChat.super.ctor(self, resource)
    self._curIndex = 1
end

function PopupStoryChat:awake()
    printDebug(" PopupStoryChat:awake()")
    UIUtil.addTouchEvent(self._imagePanel.transform, handler(self, self._onClickNext))
end

function PopupStoryChat:_onClickNext()
    printDebug(self.gameObject)
    printDebug("我要说下一句话" .. self._curIndex)
    self._curIndex = self._curIndex + 1
    if self._curIndex > #self._storyList then
        self:close()
    else
        self:setTalk(self._storyList[self._curIndex])
    end
end

function PopupStoryChat:setCallback(callback)
    self._callback = callback
end

function PopupStoryChat:updateUI(groupId)
    self._curIndex = 1
    self._spineLeft.gameObject:SetActive(false)
    self._spineRight.gameObject:SetActive(false)
    self._storyList = StoryHelper.getStoryGroup(groupId)
    self:setTalk(self._storyList[self._curIndex])
end

function PopupStoryChat:setTalk(talkCfg)
    self._textTalk.text = talkCfg.content
    if talkCfg.dir == 1 then -- 左边
        self._imageTalkBg.rectTransform.localScale = Vector3.new(1, 1, 1)
        if talkCfg.hero ~= "" then
            self._spineLeft.gameObject:SetActive(true)
            self._spineLeft:updateByResId(talkCfg.hero)
        end
    else -- 右边
        self._imageTalkBg.rectTransform.localScale = Vector3.new(-1, 1, 1)
        if talkCfg.hero ~= "" then
            self._spineRight.gameObject:SetActive(true)
            self._spineRight:updateByResId(talkCfg.hero)
        end
    end
end

function PopupStoryChat:onClose()
    if self._callback then
        self._callback()
    end
end

return PopupStoryChat
